// router - intex.ts
import { storeToRefs } from "pinia";
import { loginStore } from "@/store/login";
import { userStore } from "@/store/user";
// append import
import { MerchantsRoutesList, ProductRoutesList, OperationRoutesList, routesList, ResearchRoutesList } from "./modules/newsRouter";
import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router";
const routes: Array<RouteRecordRaw> = [
  {
    path: "/",
    redirect: "/home",
  },
  {
    path: "/home",
    name: "Home",
    redirect: "/home/index",
    component: () => import("@/views/home/home.vue"),
    beforeEnter: (_to: any, _from: any, next: any) => {
      // const login = loginStore();
      const store = userStore();
      const { user } = storeToRefs(store);
      if (user.value[0].indexOf('recruit') != -1) {
        // 招商部
        next()
      } else if (user.value[0].indexOf('superadmin') != -1) {
        // 超级管理员
        next()
      } else if (user.value[0].indexOf('stock') != -1) {
        // 产品
        // login.resetToken()
        next('/productHome/page')
      } else if (user.value[0].indexOf('core') != -1) {
        // 运营
        // login.resetToken()
        next('/operation/operatepage')
      } else if (user.value[0].indexOf('invent') != -1) {
        // 研发
        // login.resetToken()
        next('/research/researchPage')
      } else if (user.value[0].indexOf('selectionOfficer') != -1) {
        // 选品官
        next('/selection/selectionOfficer')
      } else if (user.value[0] == "app") {
        // app
        next('/forge/forgeHome')
      }
    },
    children: [
      {
        path: "index",
        name: "Index",
        component: () => import("@/views/home/index.vue"),
        meta: { title: "首页", showNavbar: false, isTabbar: true, nocache: true },
      },
      {
        path: "/productList",
        name: "productList",
        component: () => import("@/views/productList/index.vue"),
        meta: { title: "首页", showNavbar: false, isTabbar: true, keepAlive: true },
      },
      {
        path: "profile",
        name: "Profile",
        component: () => import("@/views/home/profile.vue"),
        meta: { title: "首页", showNavbar: false, isTabbar: true, nocache: true },
      },
      {
        path: "/supplierList",
        name: "supplierList",
        component: () => import("@/views/supplierList/index.vue"),
        meta: { title: "首页", showNavbar: false, isTabbar: true, nocache: true },
      },
      ...MerchantsRoutesList
    ],
  },
  // 产品部
  {
    path: "/productHome",
    name: "ProductHome",
    redirect: "/productHome/page",
    component: () => import("@/views/productHome/home.vue"),
    children: [
      {
        path: "page",
        name: "Page",
        component: () => import("@/views/productHome/index.vue"),
        meta: { title: "首页", showNavbar: false, isTabbar: true, nocache: true },
      },
      {
        path: "productLists",
        name: "ProductLists",
        component: () => import("@/views/productHome/productLists/index.vue"),
        meta: { title: "商品列表", showNavbar: false, isTabbar: true, keepAlive: true },
      },
      {
        path: "revenue",
        name: "Revenue",
        component: () => import("@/views/productHome/revenue/index.vue"),
        meta: { title: "首页", showNavbar: false, isTabbar: true, nocache: true },
      },
      {
        path: "productMine",
        name: "ProductMine",
        component: () => import("@/views/productHome/productMine/index.vue"),
        meta: { title: "首页", showNavbar: false, isTabbar: true, keepAlive: true },
      },
      ...ProductRoutesList
    ]
  },
  // 运营部
  {
    path: "/operation",
    name: "Operation",
    redirect: "/operation/operatepage",
    component: () => import("@/views/operation/home.vue"),
    children: [
      {
        path: "operatepage",
        name: "OperatePage",
        component: () => import("@/views/operation/index.vue"),
        meta: { title: "首页", showNavbar: false, isTabbar: true, nocache: true },
      },
      {
        path: "selectedProducts",
        name: "SelectedProducts",
        component: () => import("@/views/operation/selectedProducts/index.vue"),
        meta: { title: "首页", showNavbar: false, isTabbar: true, nocache: true },
      },
      {
        path: "salesList",
        name: "SalesList",
        component: () => import("@/views/operation/salesList/index.vue"),
        meta: { title: "首页", showNavbar: false, isTabbar: true },
      },
      {
        path: "operateMine",
        name: "OperateMine",
        component: () => import("@/views/operation/operateMine/index.vue"),
        meta: { title: "首页", showNavbar: false, isTabbar: true },
      },
      ...OperationRoutesList
    ]
  },
  // 运营官
  {
    path: "/selection",
    name: "Selection",
    redirect: "/selection/selectionOfficer",
    component: () => import("@/views/selection/home.vue"),
    children: [
      {
        path: "selectionOfficer",
        name: "SelectionOfficer",
        component: () => import("@/views/selection/index.vue"),
        meta: { title: "首页", showNavbar: false, isTabbar: true, nocache: true },
      },
      {
        path: "selectionProducts",
        name: "SelectionProducts",
        component: () => import("@/views/selection/selectionProducts/index.vue"),
        meta: { title: "首页", showNavbar: false, isTabbar: true, nocache: true },
      },
      {
        path: "selectionSalesList",
        name: "SelectionSalesList",
        component: () => import("@/views/selection/selectionSalesList/index.vue"),
        meta: { title: "首页", showNavbar: false, isTabbar: true },
      },
      {
        path: "selectionOperateMine",
        name: "SelectionOperateMine",
        component: () => import("@/views/selection/selectionOperateMine/index.vue"),
        meta: { title: "首页", showNavbar: false, isTabbar: true },
      },
      // ...OperationRoutesList
    ]
  },
  // 研发
  {
    path: "/research",
    name: "Research",
    redirect: "/research/researchPage",
    component: () => import("@/views/research/home.vue"),
    children: [
      {
        path: "researchPage",
        name: "ResearchPage",
        component: () => import("@/views/research/index.vue"),
        meta: { title: "首页", showNavbar: false, isTabbar: true, nocache: true },
      },
      {
        path: "researchLists",
        name: "ResearchLists",
        component: () => import("@/views/research/researchLists/index.vue"),
        meta: { title: "首页", showNavbar: false, isTabbar: true, keepAlive: true },
      },
      {
        path: "researchRevenue",
        name: "ResearchRevenue",
        component: () => import("@/views/research/researchRevenue/index.vue"),
        meta: { title: "首页", showNavbar: false, isTabbar: true, nocache: true },
      },
      {
        path: "mine",
        name: "Mine",
        component: () => import("@/views/research/mine/index.vue"),
        meta: { title: "首页", showNavbar: false, isTabbar: true, nocache: true },
      },
      ...ResearchRoutesList,
    ]
  },
  // ios专属审核账号
  {
    path: "/forge",
    name: "Forge",
    redirect: "/forge/forgeHome",
    component: () => import("@/views/forge/home.vue"),
    children: [
      {
        path: "forgeHome",
        name: "ForgeHome",
        component: () => import("@/views/forge/index.vue"),
        meta: { title: "首页", showNavbar: false, isTabbar: true },
      },
      {
        path: "forgeAdd",
        name: "ForgeAdd",
        component: () => import("@/views/forge/add/index.vue"),
        meta: { title: "首页", showNavbar: false, isTabbar: true },
      },
      {
        path: "forgeMy",
        name: "ForgeMy",
        component: () => import("@/views/forge/my/index.vue"),
        meta: { title: "首页", showNavbar: false, isTabbar: true },
      },
    ]
  },
  {
    path: "/login",
    name: "Login",
    component: () => import("@/views/login/login.vue"),
    meta: { title: "登录", showNavbar: false },
    beforeEnter: (_to: any, _from: any, next: any) => {
      const store = loginStore();
      const { token } = storeToRefs(store);
      token.value ? next("/") : next();
    },
  },
  {
    path: "/forgetPassword",
    name: "ForgetPassword",
    component: () => import("@/views/login/forgetPassword.vue"),
    meta: { title: "忘记密码", showNavbar: true },
  },
  {
    path: "/editPassword",
    name: "editPassword",
    component: () => import("@/views/login/editPassword.vue"),
    meta: { title: "首页", showNavbar: false },
  },
  {
    path: "/aboutMe",
    name: "aboutMe",
    component: () => import("@/views/login/aboutMe.vue"),
    meta: { title: "首页", showNavbar: false },
  },
  {
    path: "/register",
    name: "Register",
    component: () => import("@/views/login/register.vue"),
    meta: { title: "注册", showNavbar: true },
  },
  ...routesList
];

const router = createRouter({
  routes,
  history: createWebHashHistory(),
});

export default router;
